<?php
class Bulk_sourcingModel extends BaseModel
{
	var $table  = 'bulk_sourcing';
	var $prikey = 'bulk_id';
	var $_name  = 'bulk_sourcing';
	var $_relation = array(
			'belongs_to_bulk' => array(
				'model' => 'order',
				'type' => BELONGS_TO,
				'foreign_key'   => 'order_id',
				'reverse'  => 'has_bulk',
			),
		);
	
	//查询大货采购单列表
	function get_bulk_sourcing_list($conditions,$order,$limit){
		$order_mod=& m('order');
		 
		$fields = "bulk_id,o.order_id,o.order_sn,o.brand_name,o.merchandiser,o.buyer_name,o.factory_name,o.status,bulk_sn,bulk_status,bulk_time,if_print,o.front_pic";
		$tables = "{$this->table} bulk_sourcing " .
		"LEFT JOIN {$order_mod->table} o ON o.order_id=bulk_sourcing.item_id ";
		 
		/* 条件(WHERE) */
		$conditions = $this->_getConditions($conditions, true);
		/* 排序(ORDER BY) */
		if ($order)
		{
			$order = ' ORDER BY ' . $this->getRealFields($order);
		}
		 
		/* 分页(LIMIT) */
		$limit && $limit = ' LIMIT ' . $limit;
		if ($limit)
		{
			$this->_updateLastQueryCount("SELECT COUNT(*) as c FROM {$tables}{$conditions}");
		}
		 
		/* 完整的SQL */
		$sql = "SELECT {$fields} FROM {$tables}{$conditions}{$order}{$limit}";
		 
		$sour_list =$this->db->getAll($sql);
		return $sour_list;
	}
}